import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import { createRouter, createWebHistory } from 'vue-router'
import TaskEditor from './views/TaskEditor.vue'
import TaskList from './views/TaskList.vue'
import Login from './views/Login.vue'
import MainLayout from './components/MainLayout.vue'

const routes = [
  { path: '/home', component: MainLayout },
  { path: '/task-editor', name: 'TaskEditor', component: TaskEditor },
  { path: '/task-list', name: 'TaskList', component: TaskList },
  { path: '/login', name: 'Login', component: Login },
  { path: '/', redirect: '/home' }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫：未登录自动跳转到登录页
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('isLogin') === 'true'
  if (to.path !== '/login' && !isLogin) {
    next('/login')
  } else {
    next()
  }
})

const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.mount('#app')